import React from 'react';
import { HashRouter as Router, Switch, Route, Redirect } from 'react-router-dom';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import { AuthProvider } from './contexts/AuthContext';
import ProtectedRoute from './components/ProtectedRoute';
import Layout from './components/Layout';
import Login from './views/Login';
import Unauthorized from './views/Unauthorized';
import Dashboard from './views/Dashboard';
import SheetMusic from './views/SheetMusic';
import Events from './views/Event';
import Songs from './views/Songs';
import Members from './views/Member/index';
import Rehearsals from './views/Rehearsals';
import Checkin from './views/Checkin';
import ActivityManagement from './views/Checkin/ActivityManagement';
import MyCreatedActivities from './views/Checkin/MyCreatedActivities';
import MyActivities from './views/Checkin/MyActivities';
import CheckinRecords from './views/Checkin/CheckinRecords';
import DictionaryManagement from './views/Dictionary';
import './App.css';

// 设置dayjs为中文
dayjs.locale('zh-cn');

// 路由配置
const routes = [
  // 公开路由
  {
    path: '/login',
    component: Login,
    public: true,
  },
  {
    path: '/unauthorized',
    component: Unauthorized,
    public: true,
  },
  
  // 受保护的路由
  {
    path: '/',
    component: Dashboard,
    exact: true,
  },
  {
    path: '/sheet-music',
    component: SheetMusic,
  },
  {
    path: '/events',
    component: Events,
  },
  {
    path: '/songs',
    component: Songs,
  },
  {
    path: '/members',
    component: Members,
  },
  {
    path: '/dictionary',
    component: DictionaryManagement,
  },
  {
    path: '/rehearsals',
    component: Rehearsals,
  },
  
  // 打卡系统路由
  {
    path: '/checkin',
    component: Checkin,
    exact: true,
  },
  {
    path: '/checkin/activity-management',
    component: ActivityManagement,
  },
  {
    path: '/checkin/my-created-activities',
    component: MyCreatedActivities,
  },
  {
    path: '/checkin/my-activities',
    component: MyActivities,
  },
  {
    path: '/checkin/records',
    component: CheckinRecords,
  },
];

// 路由渲染函数
const renderRoute = (route) => {
  const { path, component: Component, exact, public: isPublic } = route;
  
  if (isPublic) {
    return (
      <Route key={path} path={path} exact={exact}>
        <Component />
      </Route>
    );
  }
  
  return (
    <Route key={path} path={path} exact={exact}>
      <ProtectedRoute>
        <Layout>
          <Component />
        </Layout>
      </ProtectedRoute>
    </Route>
  );
};

function App() {
  return (
    <ConfigProvider locale={zhCN}>
      <AuthProvider>
        <Router>
          <Switch>
            {/* 渲染所有路由 */}
            {routes.map(renderRoute)}
            
            {/* 默认重定向到首页 */}
            <Redirect to="/" />
          </Switch>
        </Router>
      </AuthProvider>
    </ConfigProvider>
  );
}

export default App; 